var t = require("../../@babel/runtime/helpers/interopRequireDefault")(require("../../@babel/runtime/regenerator")), a = require("../../@babel/runtime/helpers/asyncToGenerator"), i = getApp(), e = require("../../utils/api.js"), n = require("../../http/activityApi.js");

Page({
    onShowCounter: 0,
    activityId: "",
    shareFrom: 0,
    isOpening: !1,
    isDrawing: !1,
    giftStartY: 0,
    giftEndY: 0,
    giftDuration: 0,
    winIndex: -1,
    winEnd: !1,
    loginOpened: !1,
    data: {
        inited: !1,
        systemInfo: i.data.systemInfo,
        giftList: [],
        show: !1,
        count: 0,
        bgUrl1: "http://newimg.ap1983.com/prod/20220124/21d2f03fc6b9493c975efbaf1d1c575f.png",
        bgUrl2: "http://newimg.ap1983.com/prod/20220124/e98e6b3a245e4ba1804ede1eb860a575.png",
        bgUrl3: "http://newimg.ap1983.com/prod/20220124/f1e7661efd584fcabf8288dded96ae71.png",
        couponBgUrl: "http://newimg.ap1983.com/prod/20220128/2d87782d6856453cabe09fe6d08fd81e.png",
        shareImgUrl: "http://newimg.ap1983.com/prod/20220124/4eb1a1a55ebf40e98d8174be28036dc5.png",
        qrcodeUrl: "http://newimg.ap1983.com/prod/20220125/82128ff48d6142bd9aa3bc3a5b5172d2.png"
    },
    onLoad: function(t) {
        this.activityId = t.activityId, t.shareFrom && (this.shareFrom = t.shareFrom), this.setData({
            showHelpHimDialog: this.shareFrom && this.shareFrom != i.getUsername(),
            hasAvatar: !!i.isLogin() && i.data.loginInfo.userInfo.avatar
        }), wx.showShareMenu({
            withShareTicket: !1,
            menus: [ "shareAppMessage", "shareTimeline" ]
        });
    },
    onShow: function() {
        this.onShowCounter++, 1 == this.onShowCounter || i.isLogin() ? this.loadData() : i.returnUI();
    },
    animationOnEnd: function(t) {
        var a = this;
        if (this.winEnd) return this.isDrawing = !1, void setTimeout(function() {
            a.setData({
                showWinResultDialog: !0
            });
        }, 500);
        this.animationReset(), setTimeout(function() {
            a.animationStart();
        }, 100);
    },
    animationStart: function() {
        var t = this.giftEndY;
        -1 != this.winIndex && (t = 331 * (this.data.giftList.length - this.winIndex - 1 + 1), 
        this.winEnd = !0);
        var a = wx.createAnimation({
            duration: this.isOpening ? this.giftDuration : 10 * this.giftDuration,
            timingFunction: "linear"
        });
        a.translateY(t + "rpx").step(), this.setData({
            animationData: a.export()
        });
    },
    animationReset: function() {
        var t = wx.createAnimation({
            duration: 0
        });
        t.translateY(this.giftStartY + "rpx").step(), this.setData({
            animationData: t.export()
        });
    },
    openActRuleDialogOnTap: function() {
        this.setData({
            showActRuleDialog: !0
        });
    },
    openOrderListDialogOnTap: function() {
        this.setData({
            showOrderListDialog: !0
        });
    },
    openProbaListDialogOnTap: function() {
        this.setData({
            showProbaListDialog: !0
        });
    },
    openShareDialogOnTap: function() {
        this.setData({
            showShareDialog: !0
        });
    },
    confirmWinResultDialogOnTap: function() {
        wx.reLaunch({
            url: i.homePath
        });
    },
    closeWinResultDialogOnTap: function() {
        this.setData({
            showWinResultDialog: !1
        });
    },
    closeHelpHimOnTap: function() {
        this.setData({
            showHelpHimDialog: !1
        });
    },
    loadData: function() {
        var e = arguments, o = this;
        return a(t.default.mark(function a() {
            var s;
            return t.default.wrap(function(t) {
                for (;;) switch (t.prev = t.next) {
                  case 0:
                    return !(e.length > 0 && void 0 !== e[0]) || e[0], t.prev = 1, o.firstLoad = !1, 
                    t.next = 5, n.freeTakeHome(o.activityId);

                  case 5:
                    1 == (s = t.sent).data.status || o.data.inited || i.showAlert("提示", "当前活动已失效！"), 
                    o.giftStartY = 0, o.giftEndY = 331 * (s.data.activityCouponList.length + 1), o.giftDuration = 100 * (s.data.activityCouponList.length + 1), 
                    console.log("【动画】初始化", o.giftStartY, o.giftEndY, o.giftDuration), s.data.activityRule = s.data.activityRule.replace(/\<img/gi, '<img class="block"'), 
                    o.setData({
                        inited: !0,
                        details: s.data,
                        photoMap: s.data.photoMap,
                        giftList: s.data.activityCouponList,
                        actRule: s.data.activityRule,
                        progTotal: s.data.consumePrice,
                        shareTitle: s.data.shareTile,
                        inviteNum: s.data.inviteNum
                    }, function() {
                        o.animationStart();
                    }), 1 == s.data.status ? (o.loadHelpRank(), o.loadDrawNum(), o.loadOrderList(), 
                    o.loadPriceProgress()) : o.setData({
                        giftList: [],
                        actRule: null
                    }), t.next = 19;
                    break;

                  case 16:
                    t.prev = 16, t.t0 = t.catch(1), console.log(t.t0);

                  case 19:
                  case "end":
                    return t.stop();
                }
            }, a, null, [ [ 1, 16 ] ]);
        }))();
    },
    startOnTap: function() {
        var t = this;
        if (1 == this.data.details.status) if (0 != this.data.count) {
            if (this.data.inited && !this.isDrawing) {
                this.isDrawing = !0, this.setData({
                    press: !0
                }), this.clearAnimation("#gift-list"), setTimeout(function() {
                    t.isOpening = !0, t.animationStart();
                }, 100), this.winIndex = -1, this.winEnd = !1;
                var a = new Date().getTime();
                n.freeTakeDraw(this.activityId).then(function(i) {
                    return t.loadDrawNum(), e.delay(a, 2e3, i);
                }).then(function(a) {
                    t.data.giftList.map(function(i, e) {
                        i.couponId == a.data.couponId && (t.winIndex = e);
                    }), console.log("【抽奖】中奖序号", t.winIndex), console.log("【抽奖】中奖内容", t.data.giftList[t.winIndex]), 
                    console.log("【抽奖】中奖返回", a.Info), t.setData({
                        press: !1,
                        winResult: a.data
                    });
                }).catch(function(a) {
                    t.isDrawing = !1, t.setData({
                        press: !1
                    }), t.clearAnimation("#gift-list"), setTimeout(function() {
                        t.isOpening = !1, t.animationStart();
                    }, 100);
                });
            }
        } else i.showAlert("提示", "您暂不能参与抽奖！邀请" + this.data.inviteNum + "位好友为您助力或原价消费" + this.data.progTotal + "元均可获得抽奖次数！"); else i.showAlert("提示", "当前活动已失效！");
    },
    onGetUserInfoThenHelpHim: function(e) {
        var n = this;
        return a(t.default.mark(function a() {
            return t.default.wrap(function(t) {
                for (;;) switch (t.prev = t.next) {
                  case 0:
                    if (console.log("授权", e), -1 == e.detail.errMsg.indexOf("ok")) {
                        t.next = 14;
                        break;
                    }
                    return t.prev = 2, i.showLoading(), t.next = 6, i.uploadUserInfo(e.detail.userInfo);

                  case 6:
                    wx.hideLoading(), n.showCaptcha(), t.next = 14;
                    break;

                  case 10:
                    t.prev = 10, t.t0 = t.catch(2), console.log(t.t0), wx.hideLoading();

                  case 14:
                  case "end":
                    return t.stop();
                }
            }, a, null, [ [ 2, 10 ] ]);
        }))();
    },
    showCaptcha: function() {
        this.setData({
            show: !0
        }), this.selectComponent("#captcha").show();
    },
    helpHimOnTap: function(t) {
        var a = this;
        console.log("helpHimOnTap"), i.showLoading(), n.freeTakeHelpHim(this.activityId, this.shareFrom, t.detail.captchaVerification).then(function(t) {
            a.closeHelpHimOnTap(), i.showAlert("提示", "助力成功！");
        }).catch(function(t) {}).then(function() {
            wx.hideLoading();
        });
    },
    onShareAppMessage: function(t) {
        var a = {
            title: this.data.details.shareTile,
            path: "/pages/act-free-take/act-free-take?activityId=" + this.activityId,
            imageUrl: this.data.shareImgUrl
        };
        return "button" == t.from && (a = {
            title: "帮我助力，一起拿免费盲盒吧！点这里！",
            path: "/pages/act-free-take/act-free-take?shareFrom=" + i.getUsername() + "&activityId=" + this.activityId,
            imageUrl: this.data.shareImgUrl
        }), console.log(a), a;
    },
    loadHelpRank: function() {
        var t = this;
        n.freeTakeHelpRank(this.activityId).then(function(a) {
            t.setData({
                userList: a.data.shareMemberList
            });
        }).catch(function(t) {});
    },
    loadDrawNum: function() {
        var t = this;
        n.freeTakeDrawNum(this.activityId).then(function(a) {
            t.setData({
                count: a.data.drawNum || 0
            });
        }).catch(function(t) {});
    },
    loadOrderList: function() {
        var t = this;
        n.freeTakeOrderList(this.activityId).then(function(a) {
            t.setData({
                orderList: a.data.activityOrderList
            });
        }).catch(function(t) {});
    },
    loadPriceProgress: function() {
        var t = this;
        n.freeTakeCurPrice(this.activityId).then(function(a) {
            var i = a.data.memberActivityPrice || 0;
            i = parseFloat(i.toFixed(2)), t.setData({
                progCurrent: i
            });
        }).catch(function(t) {});
    }
});